package com.jshoperxms.service.impl;

import com.jshoperxms.controller.mall.backstage.vo.sysrole.SysRoleModuleActionRpVo;
import com.jshoperxms.controller.utils.BaseTools;
import com.jshoperxms.controller.utils.DateTools;
import com.jshoperxms.controller.utils.enums.BaseEnums;
import com.jshoperxms.controller.utils.json.GsonJson;
import com.jshoperxms.controller.utils.statickey.SerialKeys;
import com.jshoperxms.controller.utils.statickey.TagWords;
import com.jshoperxms.dao.SysRoleModuleActionRpTDao;
import com.jshoperxms.entity.SysModuleActionT;
import com.jshoperxms.entity.SysRoleModuleActionRpT;
import com.jshoperxms.service.SysRoleModuleActionRpTService;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by sdywcd on 16/10/26.
 * Des:
 */
@Service("sysRoleModuleActionRpTService")
@Scope("prototype")
public class SysRoleModuleActionRpTServiceImpl extends BaseTServiceImpl<SysRoleModuleActionRpT> implements SysRoleModuleActionRpTService {
    @Resource
    private SysRoleModuleActionRpTDao sysRoleModuleActionRpTDao;

    @Resource
    private Serial serial;

    @Transactional(propagation= Propagation.REQUIRED)
    @Override
    public void saveOrUpdateSysRoleModuleActionTransactional(SysRoleModuleActionRpT rq) {
        //删除角色下的所有权限关系
        Criterion criterion=Restrictions.eq("sysroleid",rq.getSysroleid());
        List<SysRoleModuleActionRpT>list=this.sysRoleModuleActionRpTDao.findByCriteria(SysRoleModuleActionRpT.class,criterion);
        if(!list.isEmpty()){
            this.sysRoleModuleActionRpTDao.deleteAll(list);
        }
        //添加角色和权限关系
        String ids[]= StringUtils.split(rq.getSysmoduleactionid(), TagWords.SPLITDOT);
        for(String s:ids){
            //查询某权限下是否有这个权限关系记录
            SysRoleModuleActionRpT bean=new SysRoleModuleActionRpT();
            bean.setId(this.serial.Serialid(SerialKeys.SYSROLEACTIONRPT));
            bean.setCreatetime(DateTools.getSystemTimeBi());
            bean.setCreatorid(BaseTools.getAdminCreateId());
            bean.setSysmoduleactionid(s);
            bean.setSysrolename(rq.getSysrolename());
            bean.setSysroleid(rq.getSysroleid());
            bean.setUpdatetime(DateTools.getSystemTimeBi());
            bean.setVersiont(1);
            bean.setStatus(BaseEnums.DataUsingState.USING.getState());
            this.sysRoleModuleActionRpTDao.save(bean);

        }
    }
}
